package com.tan.controller;


import com.alibaba.fastjson.JSON;
import com.tan.pojo.Menu;
import com.tan.response.Result;
import com.tan.service.impl.MenuServiceImpl;
import com.tan.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author tanRuYi
 * @since 2021-08-25
 */
@Api(tags = "菜单模块")
@RestController
@RequestMapping("/menu")

public class MenuController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    MenuServiceImpl menuService;

    @PostMapping("/gerTree")
    @ApiOperation(value = "得到树状菜单")
    public Result buildMenuTree(String username){

        List<Menu> menus = null;

        if (redisUtil.hasKey("menus")){
            menus = (List<Menu>) redisUtil.get("menus");
        }else{
            menus = menuService.buildMenuTree(username);
            redisUtil.set("menus", menus);
        }

        return Result.ok().data("data", menus);
    }
}

